package cn.wjh.business.controller;

import cn.wjh.business.domain.Collect;
import cn.wjh.business.service.ICollectService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收藏Controller
 *
 * @author jiraWu
 * @date 2023-10-21
 */
@RestController
@RequestMapping("/business/collect")
public class CollectController extends BaseController {
	@Resource
	private ICollectService collectService;

	/**
	 * 查询收藏列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:list')")
	@GetMapping("/list")
	public TableDataInfo list(Collect collect) {
		startPage();
		List<Collect> list = collectService.selectCollectList(collect);
		return getDataTable(list);
	}

	/**
	 * 查询我的收藏列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:list')")
	@GetMapping("/my")
	public TableDataInfo myList(Collect collect) {
		startPage();
		collect.setUserId(getUserId());//当前用户Id
		List<Collect> list = collectService.selectCollectList(collect);
		return getDataTable(list);
	}

	/**
	 * 导出收藏列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:export')")
	@Log(title = "收藏", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Collect collect) {
		List<Collect> list = collectService.selectCollectList(collect);
		ExcelUtil<Collect> util = new ExcelUtil<Collect>(Collect.class);
		util.exportExcel(response, list, "收藏数据");
	}

	/**
	 * 获取收藏详细信息
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:query')")
	@GetMapping(value = "/{collectId}")
	public AjaxResult getInfo(@PathVariable("collectId") Long collectId) {
		return success(collectService.selectCollectByCollectId(collectId));
	}

	/**
	 * 新增收藏
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:add')")
	@Log(title = "收藏", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Collect collect) {
		return toAjax(collectService.insertCollect(collect));
	}

	/**
	 * 修改收藏
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:edit')")
	@Log(title = "收藏", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Collect collect) {
		return toAjax(collectService.updateCollect(collect));
	}

	/**
	 * 删除收藏
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:remove')")
	@Log(title = "收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{collectIds}")
	public AjaxResult remove(@PathVariable Long[] collectIds) {
		return toAjax(collectService.deleteCollectByCollectIds(collectIds));
	}

	/**
	 * 获取收藏状态
	 */
//	@PreAuthorize("@ss.hasPermi('business:collect:isCollect')")
	@GetMapping("/status/{dataId}")
	public AjaxResult isCollect(@PathVariable Long dataId, @RequestParam(required = true) String type) {
		AjaxResult result = success();
		boolean status = collectService.isCollect(dataId, type);
		result.put(AjaxResult.DATA_TAG, status);
		return result;
	}

	/**
	 * 获取收藏数量
	 *
	 * @param dataId
	 * @param type
	 * @return
	 */
	@GetMapping("count/{dataId}")
	public AjaxResult count(@PathVariable Long dataId, @RequestParam(required = true) String type) {
		AjaxResult result = success();
		Long count = collectService.countByDataIdAndType(dataId, type);
		result.put(AjaxResult.DATA_TAG, count);
		return result;
	}

}
